﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ay.MvcFramework;
using Ay.MvcFramework.AyMarkupExtension;
using MPSystem.Controllers;
using Base.BLL;
using Ay.Framework.WPF;
using System.Windows.Threading;

namespace MPSystem.Views.Others
{
    /// <summary>
    /// FTPPage.xaml 的交互逻辑
    /// </summary>
    public partial class FTPPage : AyPage
    {
        Color colorred = (Color)ColorConverter.ConvertFromString("Red");

        private FtpService ftp = null;
        private FtpService ftp2 = null;
        /// <summary>
        /// 轮询发送间隔，单位分钟
        /// </summary>
        private static int Span
        {
            get
            {
                return ConfigService.GetIni("FtpSpan", "1").ToInt();
            }
            set
            {
                ConfigService.SetIni("FtpSpan", value.ToString());
            }
        }
        public static DispatcherTimer SendTimer=null;

        public FTPPage()
        {
            InitializeComponent();
            //矿井编号
            kjbh.Text = "000000000000000";

            ck_zcgzzl.IsChecked= ConfigService.GetIni("FtpZC", "1")=="1";
            ck_wyyd.IsChecked= ConfigService.GetIni("FtpDBLC", "1") == "1"; 
            ck_mgyl.IsChecked= ConfigService.GetIni("FtpMG", "1") == "1"; 
            ck_wyyl.IsChecked= ConfigService.GetIni("FtpCQYL", "1") == "1";
            ck_bdml.IsChecked=ConfigService.GetIni("FtpLocal","1")=="1"; connct.IsEnabled = !ck_bdml.IsChecked.Value;
            SaveDir.Text = ConfigService.GetIni("FtpSaveDir", "D:\\dataky"); btn_Dir.IsEnabled = ck_bdml.IsChecked.Value; SaveDir.IsEnabled = ck_bdml.IsChecked.Value; 
            input_dz.Text=ConfigService.GetIni("FtpIP", "127.0.0.1"); input_dz.IsEnabled = !ck_bdml.IsChecked.Value;
            input_uid.Text = ConfigService.GetIni("FtpUid", "admin"); input_uid.IsEnabled = !ck_bdml.IsChecked.Value;
            input_pwd.Password = ConfigService.GetIni("FtpPsw", "admin"); input_pwd.IsEnabled = !ck_bdml.IsChecked.Value; psw = input_pwd.Password;
            //ftp2
            uploadDir.Text = ConfigService.GetIni("FtpuploadDir", "Default");
            input_dz2.Text = ConfigService.GetIni("FtpIP2", "127.0.0.1"); input_dz2.IsEnabled = !ck_bdml.IsChecked.Value;
            input_dk2.Text = ConfigService.GetIni("FtpPort2", "21"); input_dk2.IsEnabled = !ck_bdml.IsChecked.Value;
            input_uid2.Text = ConfigService.GetIni("FtpUid2", "admin"); input_uid2.IsEnabled = !ck_bdml.IsChecked.Value;
            input_pwd2.Password = ConfigService.GetIni("FtpPsw2", "admin"); input_pwd2.IsEnabled = !ck_bdml.IsChecked.Value; psw2 = input_pwd2.Password;
            kjbh.Text = ConfigService.GetIni("KJNumber","000000000000000");
            connct2.IsEnabled = !ck_bdml.IsChecked.Value;
             
            if (SendTimer!=null && SendTimer.IsEnabled)
            {
                EnableAll(false);
                start.IsEnabled = false;
                stop.IsEnabled = true;
            }
        }
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void connct_click(object sender, RoutedEventArgs e)
        {
            if (input_dz.Text == string.Empty || input_uid.Text == string.Empty || input_pwd.Password == string.Empty)
            {
                AyMessageBox.ShowError("FTP参数不能为空！");
                return;
            }
            ftp = new FtpService(input_dz.Text, input_uid.Text, input_pwd.Password);
            if (ftp.Connectable())
            {
                AyMessageBox.ShowInformation("连接正常！");
            }
            else
            {
                AyMessageBox.ShowError("连接失败！");
                msg.Text = ftp.LastError;
                msg.Foreground = new SolidColorBrush(colorred);
            }

        }
        private void connct_Ftp2_click(object sender, RoutedEventArgs e)
        {
            if (input_dk2.Text == string.Empty || input_dz2.Text == string.Empty || input_uid2.Text == string.Empty || input_pwd2.Password == string.Empty)
            {
                AyMessageBox.ShowError("FTP2参数不能为空！");
                return;
            }
            if (kjbh.Text.Length != 15)
            {
                AyMessageBox.ShowError("矿井编号必须为15位数字！");
                return;
            }
            else
            {  //连接后，可编辑矿井编号
                kjbh.IsEnabled = false;
            }
           
            ftp2 = new FtpService(input_dz2.Text, input_uid2.Text,int.Parse(input_dk2.Text.Trim()), input_pwd2.Password);
            if (ftp2.Connectable(uploadDir.Text))
            {
                AyMessageBox.ShowInformation("连接正常！");
            }
            else
            {
                AyMessageBox.ShowError("连接失败！");
                msg2.Text = ftp2.LastError;
                msg2.Foreground = new SolidColorBrush(colorred);
            }

        }


       

        public void EnableAll(bool enabled)
        {
            ck_zcgzzl.IsEnabled = enabled;
            ck_wyyd.IsEnabled = enabled;
            ck_mgyl.IsEnabled = enabled;
            ck_wyyl.IsEnabled = enabled;
            ck_bdml.IsEnabled = enabled;
            SaveDir.IsEnabled = enabled;
            input_dz.IsEnabled = enabled;
            input_uid.IsEnabled = enabled;
            input_pwd.IsEnabled = enabled;
            btn_Dir.IsEnabled = enabled;
            connct.IsEnabled = enabled;
            //ftp2
            input_dz2.IsEnabled = enabled;
            input_dk2.IsEnabled = enabled;
            input_uid2.IsEnabled = enabled;
            input_pwd2.IsEnabled = enabled;
            connct2.IsEnabled = enabled;
        }
        public void timeCycle(object sender, EventArgs e)
        {
            if (ftp.SendData())
            {
                //start.IsEnabled = false;
                //stop.IsEnabled = true;
                msg.Text = DateTime.Now.ToString("HH:mm:ss")+"成功保存";
            }
            else
            {
                msg.Text = ftp.LastError;
                msg.Foreground = new SolidColorBrush(colorred);
            }
            if (ftp2.SendData2(kjbh.Text,uploadDir.Text))
            {
                //start.IsEnabled = false;
                //stop.IsEnabled = true;
                msg2.Text = "ftp2"+DateTime.Now.ToString("HH:mm:ss") + "成功保存";
            }
            else
            {
                msg2.Text = ftp2.LastError;
                 msg2.Foreground = new SolidColorBrush(colorred);
            }
        }

        private void start_click(object sender, RoutedEventArgs e)
        {
            startftp();
            startftp2();
        }

        private void stop_click(object sender, RoutedEventArgs e)
        {
            if (SendTimer != null)
            {
                SendTimer.Stop();
                SendTimer = null;
            }
            EnableAll(true);

            start.IsEnabled = true;
            stop.IsEnabled = false;
            //断口后，可编辑矿井编号
            kjbh.IsEnabled = true;
        }

        private void AyIconAllButton_Click(object sender, RoutedEventArgs e)
        {
            string tmp_path = "";
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.ShowDialog();
            if (fbd.SelectedPath != string.Empty)
            {
                tmp_path = fbd.SelectedPath;
                SaveDir.Text = tmp_path;
                ConfigService.SetIni("ExportDataDir", tmp_path);
                if (ftp != null)
                    ftp.SaveDir = tmp_path;
            }
        }

        private void Ck_bdml_Click(object sender, RoutedEventArgs e)
        {
            SaveDir.IsEnabled = ck_bdml.IsChecked.Value;
            btn_Dir.IsEnabled = ck_bdml.IsChecked.Value;
            input_dz.IsEnabled = !ck_bdml.IsChecked.Value;
            input_uid.IsEnabled = !ck_bdml.IsChecked.Value;
            input_pwd.IsEnabled = !ck_bdml.IsChecked.Value;
            connct.IsEnabled = !ck_bdml.IsChecked.Value;
            //FTP2
            input_dz2.IsEnabled = !ck_bdml.IsChecked.Value;
            input_uid2.IsEnabled = !ck_bdml.IsChecked.Value;
            input_pwd2.IsEnabled = !ck_bdml.IsChecked.Value;
            connct2.IsEnabled = !ck_bdml.IsChecked.Value;
            input_dk2.IsEnabled = !ck_bdml.IsChecked.Value;

        }

        private void AyPage_Unloaded(object sender, RoutedEventArgs e)
        {
            SaveIni();
            SaveIni2();
        }
        private string psw = "";
        private void Input_pwd_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if(!input_pwd.Password.IsEmpty())
                psw = input_pwd.Password;
        }
        private string psw2 = "";
        private void Input_pwd2_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (!input_pwd2.Password.IsEmpty())
                psw2 = input_pwd2.Password;
        }

        #region FTP操作
        /// <summary>
        /// 第一个FTP
        /// </summary>
        void startftp()
        {
            if (input_dz.Text == string.Empty || input_uid.Text == string.Empty || input_pwd.Password == string.Empty)
            {
                AyMessageBox.ShowError("FTP参数不能为空！");
                return;
            } 
             
            ftp = new FtpService(input_dz.Text, input_uid.Text, input_pwd.Password);
            ftp.PushZC = ck_zcgzzl.IsChecked.Value;
            ftp.PushDBLC = ck_wyyd.IsChecked.Value;
            ftp.PushMGYL = ck_mgyl.IsChecked.Value;
            ftp.PushCQYL = ck_wyyl.IsChecked.Value;
            //ck_xdbx.IsChecked.Value;//巷道变形没有
            //ftp.SaveDir = tDir.Text;
            ftp.SendLocal = ck_bdml.IsChecked.Value;
            ftp.SendFTP = !ck_bdml.IsChecked.Value;
            ftp.SaveDir = SaveDir.Text;

            SaveIni();
            EnableAll(false);

            if (SendTimer == null)
            {
                SendTimer = new DispatcherTimer() { IsEnabled = true };
                SendTimer.Tick += new EventHandler(timeCycle);
                SendTimer.Interval = TimeSpan.FromMinutes(Span);
            }
            ftp.SendIni();
            SendTimer.Start();
            start.IsEnabled = false;
            stop.IsEnabled = true;
        }
        /// <summary>
        /// 第二个FTP
        /// </summary>
        void startftp2()
        {
            //ftp2
            if (input_dz2.Text == string.Empty || input_uid2.Text == string.Empty || input_pwd2.Password == string.Empty)
            {
                AyMessageBox.ShowError("FTP2参数不能为空！");
                return;
            }
            ftp2 = new FtpService(input_dz2.Text, input_uid2.Text, input_pwd2.Password);
            ftp2.PushZC = ck_zcgzzl.IsChecked.Value;
            ftp2.PushDBLC = ck_wyyd.IsChecked.Value;
            ftp2.PushMGYL = ck_mgyl.IsChecked.Value;
            ftp2.PushCQYL = ck_wyyl.IsChecked.Value;
            //ck_xdbx.IsChecked.Value;//巷道变形没有
            //ftp.SaveDir = tDir.Text;
            ftp2.SendLocal = ck_bdml.IsChecked.Value;
            ftp2.SendFTP = !ck_bdml.IsChecked.Value;
            ftp2.SaveDir = SaveDir.Text;

            //增加ftp2配置
             SaveIni2();
           
            //if (SendTimer == null)
            //{
            //    SendTimer = new DispatcherTimer() { IsEnabled = true };
            //    SendTimer.Tick += new EventHandler(timeCycle);
            //    SendTimer.Interval = TimeSpan.FromMinutes(Span);
            //}
            ftp2.SendIni2(kjbh.Text,uploadDir.Text);
            //SendTimer.Start();
            //start.IsEnabled = false;
            //stop.IsEnabled = true;
        }

        public void SaveIni()
        {
            ConfigService.SetIni("FtpZC", ck_zcgzzl.IsChecked.Value ? "1" : "0");
            ConfigService.SetIni("FtpDBLC", ck_wyyd.IsChecked.Value ? "1" : "0");
            ConfigService.SetIni("FtpMG", ck_mgyl.IsChecked.Value ? "1" : "0");
            ConfigService.SetIni("FtpCQYL", ck_wyyl.IsChecked.Value ? "1" : "0");
            ConfigService.SetIni("FtpLocal", ck_bdml.IsChecked.Value ? "1" : "0");
            ConfigService.SetIni("FtpSaveDir", SaveDir.Text);
            ConfigService.SetIni("FtpIP", input_dz.Text);
            ConfigService.SetIni("FtpUid", input_uid.Text);
            ConfigService.SetIni("FtpPsw", psw);
            ConfigService.SetIni("KJNumber", kjbh.Text);
            ConfigService.SetIni("FtpuploadDir", uploadDir.Text);

            
        }

        public void SaveIni2()
        {
            
            ConfigService.SetIni("FtpIP2", input_dz2.Text);
            ConfigService.SetIni("FtpUid2", input_uid2.Text);
            ConfigService.SetIni("FtpPsw2", psw2);
        }

        #endregion
    }
}
